package com.supply.dl.activity;

import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Toast;

import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import com.supply.dl.R;
import com.supply.dl.activity.fragment.MeFragment;
import com.supply.dl.activity.fragment.MessageFragment;
import com.supply.dl.activity.fragment.WorkFragment;
import com.supply.dl.bottomtabbar.BottomTabBar;
import com.supply.dl.utils.StringUtils;

public class MainActivity extends BaseActivity {
    private BottomTabBar mBottomBar;

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        super.initView();
        mBottomBar = (BottomTabBar) findViewById(R.id.bottom_bar);

        setToolBarInVisable();
    }

    @Override
    protected void initListener() {
        super.initListener();
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        initTab(1);
    }


    private void initTab(int index) {
        mBottomBar.init(getSupportFragmentManager()).setChangeColor(Color.rgb(73, 104, 230), Color.rgb(170, 170, 170))//
                .setFontSize(12).setTabPadding(10, 3, 5).isShowDivider(true)//
                .setDividerColor(Color.GRAY).setDividerHeight(1.0f) //
                .addTabItem(getString(R.string.supply_message), R.mipmap.icon_supply_dl_message_pressed, R.mipmap.icon_supply_dl_message_default, MessageFragment.class)//
                .addTabItem(getString(R.string.supply_work), R.mipmap.icon_supply_dl_work_pressed, R.mipmap.icon_supply_dl_work_default, WorkFragment.class)//
                .addTabItem(getString(R.string.supply_me), R.mipmap.icon_supply_dl_me_pressed, R.mipmap.icon_supply_dl_me_default, MeFragment.class)//
                .isShowDivider(false).setCurrentTab(index)//默认选中第一项
                .setOnTabChangeListener(null);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if (result != null) {
            if (result.getContents() == null) {
                Toast.makeText(this, "扫码取消！", Toast.LENGTH_LONG).show();
            } else {
                String content = result.getContents();
                if (StringUtils.isBlank(content)) return;
                if (content.contains("http://") || content.contains("https://")) {
                    Uri uri = Uri.parse(content);
                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                    startActivity(intent);
                } else {
                    Toast.makeText(this, "扫码成功："+content, Toast.LENGTH_LONG).show();
                }
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }
}
